package com.neuedu.controller;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neuedu.api.AdminService;
import com.neuedu.common.ResultJson;
import com.neuedu.entity.Register;
import com.neuedu.entity.SettleCategory;
import com.neuedu.service.NeueduPayService;
import com.neuedu.service.RegisterService;
import com.neuedu.service.SettleCategoryService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 挂号信息表 前端控制器
 * </p>
 *
 * @author LiYang
 * @since 2024-11-11
 */
@RestController
@RequestMapping("/register")
public class RegisterController {
    @Resource
    SettleCategoryService settleCategoryService;
    @Resource
    AdminService adminService;
    @Resource
    RegisterService registerService;
    @Resource
    NeueduPayService neueduPayService;

    /**
     * 挂号数据列表
     * @param pageNo
     * @param pageSize
     * @param caseNumber
     * @param realName
     * @param visitState
     * @param openId
     * @return
     */
    @GetMapping("/list")
    ResultJson<IPage<Register>> list(Integer pageNo, Integer pageSize, String realName , String caseNumber, Integer visitState, Long openId) {
        return ResultJson.success(registerService.list(pageNo, pageSize, caseNumber, realName, openId, visitState));
    }

    /**
     * 挂号页面 获取挂号要填写的列表数据
     *
     * @return
     */
    @GetMapping("/getRegisterData")
    ResultJson<Map<String, List<?>>> getRegisterData () {
        Map<String, List<?>> result = new HashMap<>();
        result.put("settles", settleCategoryService.getActive());
        result.put("depts", adminService.getRegisterDept());
        result.put("levels", adminService.getRegistLevelActive());
        return ResultJson.success(result);
    }

    /**
     * 挂号时获取当天挂号数量
     * @param visitDate
     * @param noon
     * @param employeeId
     * @return
     */
    @GetMapping("/getRegisterCount")
    ResultJson<Integer> getRegisterCount(LocalDate visitDate, String noon, Long employeeId) {
        return ResultJson.success(registerService.getRegisterCount(visitDate, noon, employeeId));
    }

    /**
     * 挂号信息提交
     * @param register
     * @return
     * @throws AlipayApiException
     */
    @PostMapping("/add")
    ResultJson<String> add(Register register) throws AlipayApiException {
        if ("支付宝".equals(register.getRegistMethod())){
            this.registerService.add(register);
            return ResultJson.success(neueduPayService.neueduPay(register.getCaseNumber(), register.getRegistMoney(), "挂号费"));
        }else {
            return ResultJson.success(registerService.add(register),"挂号成功");
        }

    }
    @PostMapping("/updatePay")
    String updatePay(HttpServletRequest request) {
        return registerService.updatePay(request);
    }

    /**
     * 叫号语音播报
     * @param num
     * @param realName
     * @return
     */
    @GetMapping("/call")
    ResultJson<Boolean> call(Integer num, String realName) {
        return ResultJson.success(registerService.call(num, realName));
    }

    /**
     * 医生接诊 修改挂号状态
     * @param id
     * @return
     */
    @PostMapping("/jiezhen")
    ResultJson<Register> jiezhen(Integer id) {
        return ResultJson.success(registerService.jiezhen(id));
    }

    /**
     * 查看挂号信息
     * @param id
     * @return
     */
    @GetMapping("/getById")
    ResultJson<Register> getById(Integer id) {
        return ResultJson.success(registerService.getById(id));
    }
    /**
     * 展示今日已经看诊结束的挂号记录
     */
    @GetMapping("/getHistoryRegister")
    ResultJson<IPage<Register> > getHistory(Integer pageNo, Integer pageSize, String caseNumber,String realName) {
        return ResultJson.success(registerService.getRegisterByFinish(pageNo, pageSize, caseNumber,realName));
    }


    /**
     * 退号，修改状态为已退号
     * @param caseNumber
     * @return
     */
    @PostMapping("/cancelRegister")
    ResultJson<Boolean> cancelRegister(String caseNumber) {
        return ResultJson.success(registerService.cancelRegister(caseNumber),"退号成功");
    }

}
